package com.pu.controller.admin;

import com.pu.dto.CategoryDTO;
import com.pu.dto.CategoryPageQueryDTO;
import com.pu.entity.Category;
import com.pu.result.PageResult;
import com.pu.result.Result;
import com.pu.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Yusc
 * @Date 2023/10/26 15:24
 * @Version
 * @Description 菜品分类管理
 **/
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * @param categoryDTO
     * @return
     * @auther: Yusc
     * @date 2023/10/26 15:34
     * @Description 新增分类
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result<String> save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * @param categoryPageQueryDTO
     * @return
     * @auther: Yusc
     * @date 2023/10/26 15:47
     * @Description 分类分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param id
     * @auther: Yusc
     * @date 2023/10/26 16:19
     * @Description 删除分类菜品分类
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result<String> deleteById(Long id) {
        log.info("删除分类：{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * @param categoryDTO
     * @return
     * @auther: Yusc
     * @date 2023/10/26 16:49
     * @Description 修改分类
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result<String> update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类：{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * @param status
     * @param id
     * @auther: Yusc
     * @date 2023/10/26 16:55
     * @Description 启用、禁用分类
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id) {
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    /**
    * @auther: Yusc
    * @param type
    * @date 2023/10/26 17:02
    * @Description 根据类型查询分类
    */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.Categorylist(type);
        return Result.success(list);
    }

}
